<?php
include("Config.php");
include("Mysql_class.php");
include("Define.php");
include 'Page_class.php';
include_once str_replace( '\\' , '/' , realpath(dirname(__FILE__).'/../')).'/compile/index/index_template.php';
$tpl = new Template(array('debug' => true));

//获取系统设置
if(!isset($results)){
	$db = new db($conf);
	$fields = array("*");
	$results = $db->field($fields)->select("sc_system");
	$results = $results?$results[0]:array("webstate"=>0);
}
if($results['webstate']==0){
	echo "<meta charset='utf-8' />".$results['content'];exit;
}

//获取系统设置
function set($field=array("*")){
	global $conf;
	$db = new db($conf);
	$info = $db->field($field)->where('Id=1')->select("sc_set");
	return $info[0];
}
//轮播图设置
function lunbo($t){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("type=$t")->order("Id asc")->select("sc_lunbo");
		
	return $list;
}
function page1($num,$psize,$page,$url=''){
    global $conf;
    $db = new db($conf);
    if($url){        
        $pagelist = new page($num,$psize,$page,$url."&page={page}",2);
    }else{
        $pagelist = new page($num,$psize,$page,$_SERVER['PHP_SELF']."?page={page}",2);
    }  
    return $pagelist->myde_write();
}

function menu($t=0){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("ismenu"=>1,"leixing"=>$t))->order("px desc")->select("sc_catogry");
	return $result;
}
//产品搜索列表
function searchlist($page,$psize,$key=""){
	global $conf;
	$db = new db($conf);
	if(empty($key)){return false;}
	if(strlen($key)<2 || strlen($key)>30){echo "<script>alert('请输入搜索内容 长度1-10个字符');</script>";return false;}
	$list = $db->field(array("g.*","c.name"))->where("g.gname like '%".$key."%' and g.gstate=1 and g.gtotal>0")->limit($page,$psize)->order("g.Id desc")->select("sc_goods as g left join sc_catogry as c on c.Id=g.cid");
	
	return $list;
}

function searchnum($key=''){
	global $conf;
	$db = new db($conf);
	$num = $db->field(array("count(*)"))->where("gname like '%".$key."%' and gtotal>0 and gstate=1")->select("sc_goods");
	return $num[0]['count(*)'];
}

function menuinfo($cid){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("Id=$cid")->select("sc_menu");
	if($info){
		return $info[0];
	}else{
		return false;
	}
}

function menulist(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(['*'])->where("menu=1")->order("Id asc")->select("sc_menu");
	return $list;
}

function ninfo($nid){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("Id=$nid")->select("sc_news");
	if($info){
		$info[0]['content'] = htmlspecialchars_decode($info[0]['content']);
		return $info[0];
	}else{
		return false;
	}
}

function newslist($page,$psize,$cid=0){
	global $conf;
	$db = new db($conf);	
	$where = "state=1";	
	if($cid){
		$where = $where." and cid=$cid";
	}
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_news");
	for($i=0;$i<count($list);$i++){
		$list[$i]['pic1'] = json_decode($list[$i]['pic1'],true);
		$list[$i]['content'] = htmlspecialchars_decode($list[$i]['content']);
	}
	return $list;
}

function newslistnum($cid=0){
	global $conf;
	$db = new db($conf);
	$where = "state=1";	
	if($cid){
		$where = $where." and cid=$cid";
	}
	$list = $db->field(array("count(*)"))->where("state=1 and cid=$cid")->select("sc_news");
	return $list[0]['count(*)'];
}

function province(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("areapid=0")->select("sc_area");
	$str = "<option value='0'>请选择省</option>";
	for($i=0;$i<count($list);$i++){
		$str = $str."<option value=".$list[$i]['areaid'].">".$list[$i]['areaname']."</option>";
	}
	return $str;
}

//单个商品信息
function getginfo($id){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("Id=$id and gstate=1")->select("sc_goods");
	if($list){
		$set = set();
		$list[0]['gcontent'] = htmlspecialchars_decode($list[0]['gcontent']);		
		$list[0]['gthumb1'] = json_decode($list[0]['gthumb1'],true);
		
		return $list[0];
	}
	else{
		return false;
	}
}

function catelist($pid=0){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where(array("ismenu"=>1,"pid"=>0))->order("px desc")->select("sc_catogry");
	return $list;
}

function infolist($page=1,$psize=10){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where('Id<>2')->limit($page,$psize)->order("Id desc")->select("sc_info");
	return $list;
}

function infolistnum(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("count(*)"))->where('Id<>2')->select("sc_info");
	return $list[0]['count(*)'];
}

function info($id){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where(array("Id"=>intval($id)))->select("sc_info");
	return $list?$list[0]:false;
}


//分类信息
function get_cate_info($cid){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("Id=$cid")->select("sc_catogry");
	return $list?$list[0]:false;
}

//单个会员信息
function userinfo(){
	global $conf;
	$db = new db($conf);
	$id = isset($_SESSION['userid'])?intval($_SESSION['userid']):0;
	$list = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_member");	
	if($list && $list[0]['state']==1){		
		return $list[0];
	}else{
		unset($_SESSION['userid']);
		unset($_SESSION['tname']);
		unset($_SESSION['uname']);
		unset($_SESSION['state']);
		unset($_SESSION['time']);
		return false;
	}
}

//单个会员信息
function cart_address($id){
	global $conf;
	$db = new db($conf);
	if($id>0){
		$list = $db->field(array("*"))->where(array("uid"=>$_SESSION['userid'],'Id'=>$id))->select("sc_address");
	}else{
		$list = $db->field(array("*"))->where(array("uid"=>$_SESSION['userid']))->select("sc_address");
	}
	
	$dzid = "";
	$str = "";
	$tname = "";
	$phone = "";
	if($list){		
		for($i=0;$i<count($list);$i++){
			$r1 = $db->field(array("*"))->where("areaid in(".$list[$i]['province'].",".$list[$i]['city'].",".$list[$i]['county'].")")->select("sc_area");
			if($list[$i]['type']==1){
				if(count($r1)==2){
					$str =$r1[0]['areaname'].$r1[1]['areaname'].$list[$i]['address'];
				}else{
					$str = $r1[0]['areaname'].$r1[1]['areaname'].$r1[2]['areaname'].$list[$i]['address'];
				}				
			}else{
				if(count($r1)==2){
					$str = $r1[0]['areaname'].$r1[1]['areaname'].$list[$i]['address'];
				}else{
					$str = $r1[0]['areaname'].$r1[1]['areaname'].$r1[2]['areaname'].$list[$i]['address'];
				}
			}
			$tname = $list[$i]['tname'];
			$phone = $list[$i]['phone'];
			$dzid = $list[$i]['Id'];
		}
	}else{
		return false;
	}
	$return['dzid'] = $dzid;
	$return['address'] = $str;
	$return['tname'] = $tname;
	$return['phone'] = $phone;
	return $return;
}

function taglist($type){
	global $conf;
	$db = new db($conf);
	$list = $db->field(['*'])->where("level=$type and state=1")->order('px desc')->select("sc_tag");
	return $list;
}

//产品列表
function goodslist($page=1,$psize=20,$catid=0,$cplx=0,$index=0,$tag1=0,$tag2=0,$gname='',$gtype=0){
	global $conf;
	$db = new db($conf);
	$where = " gstate=1";
	if($catid){
		$where = $where." and cid=$catid ";
	}
	if($cplx && !$catid){
		$where = $where." and cplx=$cplx";
	}
	if($index==1){
	    $where = $where." and index1=1";
	}
	if($tag1>0){
		$where = $where." and tag1=$tag1";
	}
	if($tag2>0){
		$where = $where." and tag2=$tag2";
	}
	if($gname){
		$where = $where." and gname like '%".$gname."%'";
	}
	if($gtype){
		$where = $where." and type=$gtype";
	}
	$list = $db->field(array("*"))->where($where)->order("px asc")->limit($page,$psize)->select("sc_goods");
	
	
	return $list;
}

//产品列表
function goodsnum($catid=0,$cplx=0,$index=0,$tag1=0,$tag2=0,$gname='',$gtype=0){
	global $conf;
	$db = new db($conf);
	$where = " gstate=1";
	if($catid){
		$where = $where." and cid=$catid ";
	}
	if($cplx && !$catid){
		$where = $where." and cplx=$cplx";
	}
	if($index==1){
	    $where = $where." and index1=1";
	}
	if($tag1>0){
		$where = $where." and tag1=$tag1";
	}
	if($tag2>0){
		$where = $where." and tag2=$tag2";
	}
	if($gname){
		$where = $where." and gname like '%".$gname."%'";
	}
	if($gtype){
		$where = $where." and type=$gtype";
	}
	$list = $db->field(array("count(*)"))->where($where)->select("sc_goods");	
	return $list[0]['count(*)'];
}


function updateuser($uid,$data){
	global $conf;
	$db = new db($conf);
	$db->where("Id=$uid")->update("sc_member",[$data=>NULL]);	
}

//购物车列表
function cartlist(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("b.Id as bId","b.num as bnum","b.uid","b.gid","b.color","b.size","b.ticket as bticket","g.*"))->where("uid=".$_SESSION['userid'])->order("b.Id desc")->select("sc_buycart as b left join sc_goods as g on b.gid = g.Id");
	if($list){
		$info = userinfo();
		$total = $je = $kuaidi = 0;
		$set = set();
		for($i=0;$i<count($list);$i++){			
			$kdf = $list[$i]['kuaidif']*$list[$i]['bnum'];
			$list[$i]['kdf'] = $kdf;			
			
			$all = $list[$i]['cash']*$list[$i]['bnum'] + $kdf;			
			$list[$i]['all'] = $all;	
			$total = $total + $all;			
			$kuaidi = $kuaidi + $kdf;			
		}
		$list[0]['total'] = $total;
		$list[0]['kuaidi'] = $kuaidi;
	
		return $list;
	}else{
		return false;
	}
}

function success($msg,$code=0){
	if($code){
		$str = array(1,$msg,$code);
	}else{
		$str = array(1,$msg);
	}	
	echo json_encode($str);
	exit;
}

function error($msg){
	$str = array(0,$msg);
	echo json_encode($str);
	exit;
}

function getRandom($param){
    $str="0123456789";
    $key = "";
    for($i=0;$i<$param;$i++)
     {
         $key .= $str{mt_rand(0,9)};
     }
     return $key;
 }

function is_in_array($value,$array){
	if(is_array($array)){
		foreach($array as $item){
			if(is_array($item)){
				$key = array_search($value,$item);
				if($key){ return $item["name"];}
			}
		}
	}
	return false;
}

function r_post1($url = '', $param = '') {    
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();
    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($ch, CURLOPT_URL,$postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

function r_post($url = '', $param = '') {
	if (empty($url) || empty($param)) {
		return false;
	}	
	$postUrl = $url;
	$curlPost = $param;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$postUrl);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
	$data = curl_exec($ch);
	curl_close($ch);
	
	return $data;
}

//普通会员权限验证
function upower(){
	if(isset($_SESSION['userid']) && isset($_SESSION['state'])){
		if($_SESSION['state']<1){
			unset($_SESSION['userid']);
			unset($_SESSION['tname']);
			unset($_SESSION['uname']);
			unset($_SESSION['state']);
			unset($_SESSION['time']);
			if(isset($_GET['a'])){
				echo json_encode(array(2,"登录超时，请重新登录"));exit;
			}else{
				echo "<script>alert('帐号未审核或被禁用');window.location.href='/login.php';</script>";
				exit;
			}
		}
	}elseif(isset($_COOKIE['username']) && isset($_COOKIE['password'])){		
		global $conf;
		$uname = $_COOKIE['username'];
		$upass = $_COOKIE['password'];
		$db =  new db($conf);
		$field = array("*");
		$where = array("uname"=>"'".$uname."'","sjcode"=>"'".$upass."'");
		$list = $db->field($field)
				->where($where)
				->select("sc_member");
		if($list){
			if($list[0]['uname']!=$uname || $list[0]['sjcode']!=$upass){
				setcookie('username','');
				setcookie('password','');
				echo "<script>window.location.href='login.php';</script>";exit;
			}
			if($list[0]['state']==0){
				setcookie('username','');
				setcookie('password','');
				echo "<script>window.location.href='login.php';</script>";exit;
			}
			$_SESSION['userid'] = $list[0]['Id'];
			$_SESSION['tname'] = $list[0]['tname'];
			$_SESSION['uname'] = $list[0]['uname'];
			$_SESSION['state'] = $list[0]['state'];	
			$_SESSION['time'] = time();
			setcookie("username",$list[0]['uname'],time()+3600*24*365,"");
			setcookie("password",$list[0]['sjcode'],time()+3600*24*365,"");
		}else{
			setcookie('username','',time()-1000,"");
			setcookie('password','',time()-1000,"");
			echo "<script>window.location.href='/login.php';</script>";exit;
		}
	}else{
		if(isset($_GET['a'])){
			echo json_encode(array(2,"未登录"));
			exit;
		}else{			
			echo "<script>window.location.href='/login.php';</script>";
			exit;
		}
	}
}

?>